const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
    .BundleAnalyzerPlugin;
const path = require("path");
const isProduction = process.env.NODE_ENV === "production"; // 判断是否是生产环境
const cdn = {
    css: [
        "https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/css/bootstrap.min.css"
    ],
    js: [
        "https://cdn.bootcdn.net/ajax/libs/vue/3.2.37/vue.global.prod.js",
        "https://cdn.bootcdn.net/ajax/libs/vue-router/4.0.11/vue-router.global.js",
        "https://cdn.bootcdn.net/ajax/libs/vuex/4.0.2/vuex.global.js",
        "https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js",
        "https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.1.3/js/bootstrap.min.js"
    ]
};
module.exports = {
    pluginOptions: {
        "style-resources-loader": {
            preProcessor: "less",
            patterns: ["./src/style/common.less"]
        }
    },
    chainWebpack: config => {
        config.resolve.alias.set("@", path.join(__dirname, "src"));
        config.optimization.minimizer("terser").tap(args => {
            args[0].terserOptions.compress.drop_console = true;
            return args;
        });
        config.plugin("webpack-bundle-analyzer").use(BundleAnalyzerPlugin);
        if (isProduction) {
            // 压缩代码
            config.optimization.minimize(true);
            // 分割代码
            config.optimization.splitChunks({
                chunks: "all"
            });
            // 生产环境注入cdn
            config.plugin("html").tap(args => {
                args[0].cdn = cdn;
                return args;
            });
            // 生产环境下去除console
            config.optimization.minimizer("terser").tap(arg => {
                arg[0].terserOptions.compress.drop_console = true;
                return arg;
            });
        }
    },
    configureWebpack: config => {
        if (isProduction) {
            // 用cdn方式引入
            config.externals = {
                vue: "Vue",
                vuex: "Vuex",
                "vue-router": "VueRouter",
                axios: "axios",
                bootstrap: "bootstrap"
            };
        }
        // else {
        // 为开发环境修改配置...}
        // }
    }
};